//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by apple on 15/11/8.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //由于 tabBar是只读 不能够直接操作,如果要修改  可以使用KVC
        let mainTabBar = MainTabBar()
        //KVC 赋值
        setValue(mainTabBar, forKey: "tabBar")
        //加载控制器
        addChildViewControllers()
        
        mainTabBar.composeBtn.addTarget(self, action: "composeDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    //MARK: 监听方法
    @objc func composeDidClick(){
        print(__FUNCTION__)
    }
    
    func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(DiscoverTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(MessageTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    //MARK: 重载 添加控制器封装方法
    private func addChildViewController(vc: UIViewController,title: String,imageName: String) {
        
        //设置主体颜色
        self.tabBar.tintColor = UIColor.orangeColor()
        
        //实例化一个导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //添加导航控制器
        addChildViewController(nav)
    }
}
